package com.user.Controller;
import com.common.util.Md5Util;
import com.github.pagehelper.PageInfo;
import com.user.Pojo.User;
import com.user.Service.UserService;
import com.common.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
//注册
    @PostMapping("/register")
    public Result UserRegister(@RequestBody User user){
        User u=userService.UserIFNull(user.getUserName());
        if (u!=null){
            return Result.error("当前用户已注册！！！");
        }else{
            userService.AddUser(user);
            return Result.success();
        }
    }
//登录
    @PostMapping("/login")
    public Result UserLogin(@RequestBody User user){
        User u=userService.UserIFNull(user.getUserName());
        if (u==null){
            return Result.error("用户名或密码错误!!!");
        }
        if(!Md5Util.getMD5String(user.getPassword()).equals(u.getPassword())){
            return Result.error("用户名或密码错误!!!");
        }
        return Result.success(u);
    }
    //查询
    @GetMapping("/GetUser")
    public Result GetUser(@RequestParam Integer pageNum,
                          @RequestParam Integer pageSize,
                          @RequestParam String userName){
        PageInfo<User> userPageInfo=userService.GetUser(pageNum,pageSize,userName);
        return  Result.success(userPageInfo);
    }
    //成员删除
    @DeleteMapping("/UserDelete")
    public Result UserDelete(@RequestParam Integer Id){
        userService.UserDelete(Id);
        return Result.success();
    }
}
